﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Windows.Threading;

namespace Code4WP7SDK.Behaviors
{
    public class BouncingBehavior : Behavior<Border>
    {
        //方向
        private bool right = false;
        private bool left = false;
        private bool top = false;
        private bool bottom = false;
        //随机数
        private Random random = new Random();

        protected override void OnAttached()
        {
            base.OnAttached();
            //在这里可以注册不同的事件让其支持游走效果
            this.AssociatedObject.MouseLeftButtonDown += AssociatedObject_MouseLeftButtonDown;
        }

        void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(50); //重复间隔
            dispatcherTimer.Start();
        }


        void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            Border border = this.AssociatedObject;
            //先判断任意的运动方向
            JudgeDirection();
            //当前坐标
            double currentX = Canvas.GetLeft(border); //X坐标
            double currentY = Canvas.GetTop(border);  //Y坐标
            //生成随机数步长
            double endStep = random.Next(1, 10);
            //X坐标处理
            if (currentX <= 0)
            {
                currentX += endStep;
                right = true;
                left = false;
            }
            else if (currentX < 150 && currentX > 0)
            {
                if (right)
                {
                    currentX += endStep;
                }
                else if (left)
                {
                    currentX -= endStep;
                }
            }
            else if (currentX >= 150 && currentX < 300)
            {
                if (right)
                {
                    currentX += endStep;
                }
                else if (left)
                {
                    currentX -= endStep;
                }
            }
            else if (currentX >= 300)
            {
                currentX -= endStep;
                left = true;
                right = false;
            }
            //Y坐标处理
            if (currentY <= 0)
            {
                currentY += endStep;
                bottom = true; //往下走
                top = false;
            }
            else if (currentY < 100 && currentY > 0)
            {
                if (bottom)
                {
                    currentY += endStep;
                }
                else if (top)
                {
                    currentY -= endStep;
                }
            }
            else if (currentY >= 100 && currentY < 200)
            {
                if (bottom)
                {
                    currentY += endStep;
                }
                else if (top)
                {
                    currentY -= endStep;
                }
            }
            else if (currentY >= 200)
            {
                currentY -= endStep;
                top = true;
                bottom = false;
            }
            Canvas.SetLeft(border, currentX);
            Canvas.SetTop(border, currentY);
        }

        private void JudgeDirection()
        {
            Border border = this.AssociatedObject;
            //当前坐标
            double currentX = Canvas.GetLeft(border); //X坐标
            double currentY = Canvas.GetTop(border);  //Y坐标
            double beginStep = random.Next(-10, 10);//生成随机数确定移动的方向

            //先去在X坐标和Y坐标上分别取加一个随机数来确定移动的方向
            if (currentX == 150 && !right && !left)
            {
                currentX += beginStep;
                if (currentX < 150)
                {
                    left = true;
                    right = false;
                }
                else if (currentX >= 150)
                {
                    right = true;
                    left = false;
                }
            }
            if (currentY == 100 && !top && !bottom)
            {
                currentY += beginStep;
                if (currentY < 100)
                {
                    top = true;
                    bottom = false;
                }
                else if (currentY >= 100)
                {
                    bottom = true;
                    top = false;
                }
            }
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.AssociatedObject.MouseLeftButtonDown -= AssociatedObject_MouseLeftButtonDown;
        }
    }
}
